package com.tour.modules.web.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

import com.tour.common.utils.PageUtils;
import com.tour.common.utils.Query;
import com.tour.common.utils.R;
import com.tour.modules.web.constant.FileDressConstant;
import com.tour.modules.web.entity.TInfoEntity;
import com.tour.modules.web.service.TInfoService;
import com.tour.modules.web.until.UUIDGenerator;
import com.tour.modules.web.until.UpDownUntil;
import com.tour.modules.web.vo.TInfovVo;

/**
 * 景点信息表
 * 
 * @author chenshun
 * @email sunlightcs@gmail.com
 * @date 2018-01-06 22:44:15
 */
@RestController
@RequestMapping("tinfo")
public class TInfoController {
	@Autowired
	private TInfoService tInfoService;

	/**
	 * 列表
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);

		List<TInfoEntity> tInfoList = tInfoService.queryList(query);
		int total = tInfoService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(tInfoList, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 信息
	 */
	@RequestMapping("/info/{id}")
	public R info(@PathVariable("id") Long id) {
		TInfoEntity tInfo = tInfoService.queryObject(id);

		return R.ok().put("tInfo", tInfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody TInfoEntity tInfo) {
		tInfoService.save(tInfo);

		return R.ok().put("tInfo", tInfo);
	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody TInfoEntity tInfo) {
		tInfoService.update(tInfo);

		return R.ok().put("tInfo", tInfo);
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody Long[] ids) {
		tInfoService.deleteBatch(ids);

		return R.ok();
	}

	/**
	 * 查询所有信息
	 */
	@RequestMapping("/listAll")
	public R listAll(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);

		List<TInfovVo> infovVos = tInfoService.queryListAll(query);

		int total = tInfoService.queryTotal(query);

		PageUtils pageUtil = new PageUtils(infovVos, total, query.getLimit(), query.getPage());

		return R.ok().put("page", pageUtil);

	}

	/**
	 * 保存图片
	 */
	@RequestMapping("tInfoImage/{id}")
	public R tCityImage(@PathVariable Long id, @RequestParam CommonsMultipartFile image, HttpServletRequest request) {

		String path = FileDressConstant.IMAGE_UPLOAD;

		String fileAdress = FileDressConstant.File_Address + FileDressConstant.IMAGE_UPLOAD;

		String saveFileName = UUIDGenerator.getUUID();

		Long fileSize = 5 * 1024 * 1024l;
		UpDownUntil downUntil = new UpDownUntil();
		R r = downUntil.Upload(image, fileAdress, saveFileName, fileSize);

		TInfoEntity tInfoEntity = new TInfoEntity();
		tInfoEntity.setId(id);

		// 临时存储文件名
		String OriginalFilename = image.getOriginalFilename();
		String fileSuffix = OriginalFilename.substring(OriginalFilename.lastIndexOf(".") + 1).toLowerCase();
		String newname = "../../" + path + saveFileName + "." + fileSuffix;

		tInfoEntity.setTourImage(newname);
		tInfoService.update(tInfoEntity);

		return R.ok();

	}
}
